﻿Imports Data

Public Class Estados

    Private Property id_estado As Integer

    Private Sub Estados_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.CargarLV()
        Me.CargarCombobox()
    End Sub


    Private Function CargarLV() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Me.lvEstados.Items.Clear()
                Dim lista = (From x In db.Estado _
                                                     Select x.id, x.Tipo_Estado.nombre_accion, x.nombre).ToList
                For Each item In lista
                    Me.lvEstados.Items.Add(New ListViewItem(New String() {item.id, item.nombre_accion, item.nombre}) With {.Name = item.id})
                Next
            End Using
        Catch ex As Exception

        End Try
    End Function

    Private Function CargarCombobox() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of AbstractObject) = (From x In db.Tipo_Estado _
                                                          Select New AbstractObject With {.ID = x.id, _
                                                                                          .Nombre = x.nombre_accion}).Distinct.ToList

                Dim manager As BindingManagerBase = Me.cbxTipoEstado.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxTipoEstado.DisplayMember = "Nombre"
                Me.cbxTipoEstado.ValueMember = "ID"
                Me.cbxTipoEstado.DataSource = lista
                Me.cbxTipoEstado.Enabled = True
                manager.ResumeBinding()

            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function



    Private Sub IngresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles IngresarToolStripMenuItem.Click
        Me.LimpiarInterfaz()
    End Sub

    Private Function LimpiarInterfaz() As Boolean
        Try
            Me.txtNombre.Text = String.Empty
            Me.txtNombre.Focus()
            Me.btnIngresar.Text = "Ingresar"
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private Sub ModificarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ModificarToolStripMenuItem.Click
        Try
            Me.id_estado = Me.lvEstados.SelectedItems(0).Name
            Using db As New SerigraphicaDBDataContext
                Dim estado As Data.Estado = db.Estado.Where(Function(p) p.id = id_estado).Select(Function(p) p).FirstOrDefault
                Me.txtNombre.Text = estado.nombre
                Me.cbxTipoEstado.SelectedValue = estado.id_tipo_estado
                Me.btnIngresar.Text = "Modificar"
            End Using
        Catch ex As Exception

        End Try
    End Sub

    Private Sub EliminarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        Try
            Me.id_estado = Me.lvEstados.SelectedItems(0).Name
            Using db As New SerigraphicaDBDataContext
                Dim estado As Data.Estado = db.Estado.Where(Function(p) p.id = id_estado).Select(Function(p) p).FirstOrDefault
                db.Estado.DeleteOnSubmit(estado)
                db.SubmitChanges()
                MsgBox("Eliminacion Exitosa")
                Me.CargarLV()
                Me.LimpiarInterfaz()
            End Using
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnIngresar_Click(sender As System.Object, e As System.EventArgs) Handles btnIngresar.Click
        Try
            If Me.btnIngresar.Text = "Ingresar" Then
                If Me.Ingresar Then
                    MsgBox("Ingresado Correctamente")
                    Me.CargarLV()
                    Me.LimpiarInterfaz()
                End If
            Else
                If Me.Modificar Then
                    MsgBox("Modificacion Exitosa")
                    Me.CargarLV()
                    Me.LimpiarInterfaz()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function Ingresar() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim estado As New Data.Estado
                estado.nombre = txtNombre.Text
                estado.id_tipo_estado = Me.cbxTipoEstado.SelectedValue
                db.Estado.InsertOnSubmit(estado)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function Modificar() As Boolean
        Try
            Me.id_estado = Me.lvEstados.SelectedItems(0).Name
            Using db As New SerigraphicaDBDataContext
                Dim estado As Data.Estado = db.Estado.Where(Function(p) p.id = id_estado).Select(Function(p) p).FirstOrDefault
                estado.nombre = Me.txtNombre.Text
                estado.id_tipo_estado = Me.cbxTipoEstado.SelectedValue
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class